package com.magicborrow.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.activity.CommonViewPagerActivity;
import com.magicborrow.activity.HomepageActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.TagBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class BorrowingAdapter extends RefreshAdapter<StuffBean> {
    private DisplayImageOptions options;

    public BorrowingAdapter(ArrayList<StuffBean> data) {

        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateItem(ViewGroup parent, int viewType) {
        return new BorrowingViewHolder(LayoutInflater.from(context).inflate(R.layout.borrowing_item, null));
    }

    @Override
    public void onBindItem(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof BorrowingViewHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, BorrowStuffDetailActivity.class);
                    intent.putExtra("data", data.get(position));
                    context.startActivity(intent);

                }
            });

            ((BorrowingViewHolder) holder).tvName.setText(data.get(position).getUser().getNickname());
            ((BorrowingViewHolder) holder).rbPingfen.setRating(data.get(position).getUser().getLendScaleAVG());
            ((BorrowingViewHolder) holder).tvPingFen.setText("" + StringUtil.double2String(data.get(position).getUser().getLendScaleAVG()) + "（" + data.get(position).getCommentCount() + "人评价）");
            if (options == null)
                options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

            ImageLoader.getInstance().displayImage(Constants.ADDRESS + data.get(position).getUser().getAvatar80(),
                    ((BorrowingViewHolder) holder).imvHead, options);
            ((BorrowingViewHolder) holder).llImg.removeAllViews();
            final ArrayList<String> imgUrls = new ArrayList<>();
            for (StuffBean.ImagesEntity img : data.get(position).getImages()) {
                imgUrls.add(img.getOrigin());

            }
            for (int i = 0; i < imgUrls.size(); i++) {
                ImageView imv = createImageView(data.get(position).getImages().get(i));
                final int finalI = i;
                imv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                   /*     position = getIntent().getIntExtra("position", 0);
                        imageUrlList = getIntent().getStringArrayListExtra("netImageUrls");*/
                        Intent intent = new Intent(context, CommonViewPagerActivity.class);
                        intent.putExtra("position", finalI);
                        intent.putExtra("netImageUrls", imgUrls);
                        context.startActivity(intent);

                    }
                });
                ((BorrowingViewHolder) holder).llImg.addView(imv);
            }
            ((BorrowingViewHolder) holder).tvAddress.setText(data.get(position).getAddress());
            ((BorrowingViewHolder) holder).stuffName.setText(data.get(position).getName());
            ((BorrowingViewHolder) holder).tvPrice.setText("￥" + data.get(position).getPrice() + "/" + PriceModeEnum.getModeStr(data.get(position).getPriceMode()));
            ((BorrowingViewHolder) holder).llTag.removeAllViews();
            ((BorrowingViewHolder) holder).tvInfo.setText(data.get(position).getDescription());
            for (TagBean s : data.get(position).getTags())
                ((BorrowingViewHolder) holder).llTag.addView(createTag(s.getName()));


            //进入到他的主页
            ((BorrowingViewHolder) holder).imvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, HomepageActivity.class);
                    intent.putExtra("id", data.get(position).getUser().getId());
                    context.startActivity(intent);
                }
            });
        }

    }

    private TextView createTag(String tag) {
        TextView tv = new TextView(context);
        tv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.tag_bg));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimensionPixelSize(R.dimen.text_size_small_s));
        tv.setTextColor(context.getResources().getColor(R.color.orange_red));
        tv.setText("" + tag);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, context.getResources().getDimensionPixelOffset(R.dimen._5dp), 0);
        tv.setLayoutParams(params);
        return tv;
    }

    @NonNull
    private ImageView createImageView(StuffBean.ImagesEntity imagesEntity) {
        final ImageView imv = new ImageView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.setMargins(context.getResources().getDimensionPixelOffset(R.dimen.layout_padding_nor), 0, 0, 0);
        imv.setScaleType(ImageView.ScaleType.FIT_XY);
        params.width = imagesEntity.getWidth() * context.getResources().getDimensionPixelOffset(R.dimen._150dp) / imagesEntity.getHeight();
        imv.setLayoutParams(params);
        ImageLoader.getInstance().displayImage(Constants.ADDRESS + imagesEntity.getOrigin(), imv);
        return imv;
    }

    public class BorrowingViewHolder extends RecyclerView.ViewHolder {
        private TextView tvAddress;
        private TextView tvPrice;
        private TextView tvPingFen;
        private ImageView imvHead;
        private TextView tvName;
        private RatingBar rbPingfen;
        private LinearLayout llImg;
        private TextView stuffName;
        private TextView tvInfo;
        private LinearLayout llTag;

        public BorrowingViewHolder(View view) {
            super(view);
            imvHead = (ImageView) view.findViewById(R.id.imv_head);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvAddress = (TextView) view.findViewById(R.id.tv_address);
            tvPingFen = (TextView) view.findViewById(R.id.tv_pingfen);
            tvPrice = (TextView) view.findViewById(R.id.tv_price);
            tvInfo = (TextView) view.findViewById(R.id.tv_info);
            rbPingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
            llImg = (LinearLayout) view.findViewById(R.id.ll_img);
            stuffName = (TextView) view.findViewById(R.id.stuff_name);
            llTag = (LinearLayout) view.findViewById(R.id.ll_tag);
        }
    }
}
